﻿using MediatR;
using WarehouseManagement.Application.Command.RequestTransferCommand;
using WarehouseManagement.Domain.DTO.RequestTransfer;
using WarehouseManagement.Domain.Entity;
using WarehouseManagement.Infrstructre.Interface;

namespace WarehouseManagement.Application.CommandHandler.RequestTransferCommandHandlers
{
    /// <summary>
    /// 反填调拨申请
    /// </summary>
    public class RequestTransferFanCommandHandlers:IRequestHandler<RequestTransferFanCommand, FanRequestTransferDto>
    {
        private readonly IRequestTransferRepository _requestTransferRep;
        private readonly IAllocationProductAppRepository _allocationProductAppRep;

        public RequestTransferFanCommandHandlers(IRequestTransferRepository requestTransferRep, IAllocationProductAppRepository allocationProductAppRep)
        {
            _requestTransferRep = requestTransferRep;
            _allocationProductAppRep = allocationProductAppRep;
        }

        public async Task<FanRequestTransferDto> Handle(RequestTransferFanCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await _requestTransferRep.GetAll() select a).Where(t => t.RequestTransfersId == request.RequestTransfersId).ToList();
            var item = (from b in await _allocationProductAppRep.GetAll() select b).ToList();
            var result = (from c in list
                          select new FanRequestTransferDto
                          {
                              RequestTransfersId = c.RequestTransfersId,
                              RTName = c.RTName,
                              RTNumber = c.RTNumber,
                              RTAuditors = c.RTAuditors,
                              RTSmark = c.RTSmark,
                              AddName = c.AddName,
                              AddTime = c.AddTime,
                              UpdName = c.UpdName,
                              UpdTime = c.UpdTime,
                              Office_Id = c.Office_Id,
                              User_Id = c.User_Id,
                              upddto = (from d in item
                                        where d.RTNumber == c.RTNumber
                                        select new FanAllocationProductAppDto
                                        {
                                            AllocationProductAppId = d.AllocationProductAppId,
                                            RTNumber = d.RTNumber,
                                            ProductId = d.ProductId,
                                            PNumber = d.PNumber,
                                            PName = d.PName,
                                            PSpec = d.PSpec,
                                            PUnit = d.PUnit,
                                            APANumberApplication = d.APANumberApplication,
                                            APASmark = d.APASmark,
                                            AddName = d.AddName,
                                            AddTime = d.AddTime,
                                            UpdName = d.UpdName,
                                            UpdTime = d.UpdTime,
                                            Office_Id = d.Office_Id,
                                            User_Id = d.User_Id,
                                        }).ToList()
                          });
            return result.FirstOrDefault();
        }
    }
}
